package com.bole.springboot_webflux.reactivestream;

import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;

public class ReactiveStreamDemo {

    public static void main(String[] args) throws InterruptedException {
        // 1. 创建一个发布者
        SubmissionPublisher publisher = new SubmissionPublisher();
        // 2. 创建一个订阅者
        Flow.Subscriber subscriber = new Flow.Subscriber() {

            Flow.Subscription subscription;

            @Override
            public void onSubscribe(Flow.Subscription subscription) {
                System.out.println("建立订阅关系");
                this.subscription = subscription;
                subscription.request(1); // 第一次需要
            }

            @Override
            public void onNext(Object item) {
                System.out.println("接收数据:" + item);
                //业务处理
                subscription.request(10); // 被压
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("发生错误了");
            }
            @Override
            public void onComplete() {
                System.out.println("数据接收完");
            }
        };
        //3. 建立订阅者
        publisher.subscribe(subscriber);
        // 4. 发送数据
        publisher.submit("hello reactive stream");

        // 阻塞
        Thread.currentThread().join();


    }






}
